package com.hmyuan.util;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.hmyuan.domain.Article;
import com.hmyuan.domain.Category;
import com.hmyuan.service.ArticleService;
import com.hmyuan.service.CategoryService;

public class PageUtil {
	
	@Resource
	ArticleService articleService;
	@Resource
	CategoryService categoryService;
	@Resource
	DictionaryUtil dictionaryUtil;
	
	
	public void headPageListInit(){
		headPageListInitSingle();
		headPageListInitCategory();
		fileDomainName();
		titleAndDescriptAndKeywordOfSite();
	}
	/**
	 * 导航列表初始化1
	 */
	public void headPageListInitSingle(){
		Article article = new Article();
		article.setStatus(2);
		//获取application对象
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    
		ServletContext servletContext = webApplicationContext.getServletContext();  
		servletContext.setAttribute("headPageList", articleService.selectList(article));
	}
	/**
	 * 导航列表初始化2
	 */
	public void headPageListInitCategory(){
		//获取application对象
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    
		ServletContext servletContext = webApplicationContext.getServletContext();  
		servletContext.setAttribute("headPageCategoryList", categoryService.selectListWithCount());
	}
	/**
	 * 网站描述初始化
	 */
	public void titleAndDescriptAndKeywordOfSite(){
		//获取application对象
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    
		ServletContext servletContext = webApplicationContext.getServletContext();  
		servletContext.setAttribute("siteTitle", "坏银的博客");
		servletContext.setAttribute("siteKeyword", "苏小飞的博客,个人博客,IT博客,java,服务器,数据库");
		servletContext.setAttribute("siteDescript", "苏小飞的个人博客，记录工作和学习中遇到的相关问题和解决方法，记录一个程序员的成长。");
	}
	/**
	 * 文件域名配置
	 */
	public void fileDomainName(){
		//获取当前配置的文件存储位置
		String fileConfig = dictionaryUtil.checkAndInit("baseInfo").get("fileConfig");
		//根据配置获取文件服务器域名
		String domainName = dictionaryUtil.checkAndInit(fileConfig).get("domainName");
		//获取application对象
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    
		ServletContext servletContext = webApplicationContext.getServletContext();  
		servletContext.setAttribute("fileDomainName", domainName);
	}
	/**
	 *获取aplication中的值 
	 */
	public Object getValueOfAplication(String key){
		//获取application对象
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    
		ServletContext servletContext = webApplicationContext.getServletContext();  
		return servletContext.getAttribute(key);
	}
	
}
